package com.ruoyi.system.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.ProductTest;
import com.ruoyi.system.domain.vo.ProductTestImportVo;
import com.ruoyi.system.service.IProductTestService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 产品test导入监听器
 *
 * @author ruoyi
 */
@Slf4j
public class ProductTestImportListener extends AnalysisEventListener<ProductTestImportVo> implements ExcelListener<ProductTestImportVo> {

    private final IProductTestService productTestService;

    private final Boolean isUpdateSupport;

    private final String operName;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public ProductTestImportListener(Boolean isUpdateSupport) {
        this.productTestService = SpringUtils.getBean(IProductTestService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void invoke(ProductTestImportVo productTestVo, AnalysisContext context) {
        ProductTest productTest = productTestService.selectProductTestById(productTestVo.getId());
        try {
            // 验证是否存在这个产品
            if (ObjectUtil.isNull(productTest)) {
                productTest = BeanUtil.toBean(productTestVo, ProductTest.class);
                ValidatorUtils.validate(productTest);
                productTest.setCreateBy(operName);
                productTestService.insertProductTest(productTest);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、产品 ").append(productTest.getName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long id = productTest.getId();
                productTest = BeanUtil.toBean(productTestVo, ProductTest.class);
                productTest.setId(id);
                ValidatorUtils.validate(productTest);
                productTest.setUpdateBy(operName);
                productTestService.updateProductTest(productTest);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、产品 ").append(productTest.getName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、产品 ").append(productTest.getName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、产品 " + productTest.getName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<ProductTestImportVo> getExcelResult() {
        return new ExcelResult<ProductTestImportVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<ProductTestImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
} 